package com.ruoyi.customerSample.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.testEqu.TestedEqu;
import com.ruoyi.customerSample.service.ITestedEquService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 受检设备大类Controller
 * 
 * @author luguanqing
 * @date 2020-11-16
 */
@RestController
@RequestMapping("/custSam/testedEqu")
@Api(value="受检设备",tags = "受检设备接口")
public class TestedEquController extends BaseController
{
    @Autowired
    private ITestedEquService testedEquService;

    /**
     * 获取设备大类列表
     */
    @GetMapping("/list")
    @ApiOperation(value="获取设备大类列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "页数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true, dataType = "String"),
    })
    public AjaxResult list(TestedEqu reportEqu)
    {
        List<TestedEqu> reportEqus = testedEquService.selectReportEquList(reportEqu);
        return AjaxResult.success(reportEqus);
    }

    /**
     * 获取设备下拉树列表
     */
    @GetMapping("/treeselect")
    @ApiOperation(value="获取设备下拉树列表")
    public AjaxResult treeselect(TestedEqu reportEqu)
    {
        List<TestedEqu> reportEqus = testedEquService.selectReportEquList(reportEqu);
        List<TreeSelect> reportEqusTree = testedEquService.buildReportEquTreeSelect(reportEqus);
        return AjaxResult.success(reportEqusTree);
    }

    /**
     * 获取除父级的设备列表
     */
    @GetMapping("/getEquAll")
    @ApiOperation(value="获取除父级的设备列表")
    public AjaxResult getEquAll(TestedEqu reportEqu)
    {
        List<TestedEqu> reportEqus = testedEquService.selectReportEquList(reportEqu);
        reportEqus = reportEqus.stream().filter(t -> !t.getPid().equals("0")).collect(Collectors.toList());
        return AjaxResult.success(reportEqus.stream().map(TestedEqu::getName).distinct().collect(Collectors.toList()));
    }


    /**
     * 获取受检单位设备信息详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value="获取受检单位设备信息详细信息")
    @ApiImplicitParam(paramType="query", name = "id", value = "主键", required = true, dataType = "String")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(testedEquService.selectReportEquById(id));
    }


    /**
     * 根据ids获取设备名称
     */
    @GetMapping(value = "/selectReportEquByIds/{ids}")
    @ApiOperation(value="根据ids获取设备名称")
    public AjaxResult selectReportEquByIds(@PathVariable String[] ids) {
        return AjaxResult.success(testedEquService.selectReportEquByIds(ids));
    }

    /**
     * 新增受检单位设备分类信息
     */
    @PostMapping
    @ApiOperation(value="新增受检单位设备分类信息")
    public AjaxResult add(@ApiParam(name="受检单位设备",value="传入json格式",required=true)@RequestBody TestedEqu reportEqu)
    {
        return toAjax(testedEquService.insertReportEqu(reportEqu));
    }

    /**
     * 修改受检单位设备分类信息
     */
    @PutMapping
    @ApiOperation(value="新增受检单位设备分类信息")
    public AjaxResult edit(@ApiParam(name="受检单位设备",value="传入json格式",required=true)@RequestBody TestedEqu reportEqu)
    {
        return toAjax(testedEquService.updateReportEqu(reportEqu));
    }


    /**
     * 删除受检设备大类
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value="删除受检设备大类")
    public AjaxResult remove(@PathVariable String id)
    {
        return toAjax(testedEquService.deleteReportEquById(id));
    }

}
